package com.android.projetihm.activity.main;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.projetihm.R;
import com.android.projetihm.nf.Graphe;

public class MainListAdapter
		extends ArrayAdapter<Graphe>
{
	//+++++++++++//
	// CONSTANTS //
	//+++++++++++//

	//++++++++++++//
	// ATTRIBUTES //
	//++++++++++++//

	private int mResource;
	

	//++++++++++++++//
	// CONSTRUCTORS //
	//++++++++++++++//

	public MainListAdapter(
			Context context,
			int resourceId,
			List<Graphe> items)
	{
		super(context, resourceId, items);

        this.mResource = resourceId;
	}
	
	
	//+++++++++//
	// METHODS //
	//+++++++++//
	
	@Override
	public View getView(
			int position,
			View convertView,
			ViewGroup parent)
	{
        Graphe item = getItem(position);
        
        LinearLayout listView;

        if (convertView == null)
        {
        	listView = new LinearLayout(getContext());
            String inflater = Context.LAYOUT_INFLATER_SERVICE;
            LayoutInflater layoutInflater =
            		(LayoutInflater) getContext().getSystemService(inflater);
            layoutInflater.inflate(this.mResource, listView, true);

            TextView text = (TextView) listView.
            		findViewById(R.id.id_fragment_main_folder);
            text.setText(item.getFolderTitle());
            
            LinearLayout imagesContainer = (LinearLayout) listView.
            		findViewById(R.id.id_fragment_main_imagelist);
            
            ImageDownloader downloader = new ImageDownloader(imagesContainer);
            for (Integer id : item.getItems())
            {
            	downloader.download(String.valueOf(id));
            }
            
        }
        else
        {
        	listView = (LinearLayout) convertView;
        }

        return listView;
	}
	

	//++++++++++++++++//
	// STATIC METHODS //
	//++++++++++++++++//

	 
    static class ImagesViewHolder
    {
        TextView text;
        LinearLayout imagesContainer;
    }
}
